/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.CTPhieuBanHangDTO;

/**
 *
 * @author tuan
 */
public class CTPhieuBanHangDAO {

    public CTPhieuBanHangDAO() {
    }
    
    public ArrayList<CTPhieuBanHangDTO> GetCTPhieuBanHangByMaPBH(String MaPBH) throws SQLException {
        ArrayList<CTPhieuBanHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaPBH };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuBHByMaPhieuMHFromDatabase( ? )", objs);
        CTPhieuBanHangDTO ctPhieuBH;
        while (result.next()) {
            ctPhieuBH = new CTPhieuBanHangDTO(result.getString("MAPHIEUBH"), result.getString("MASP"), 
                    result.getInt("SOLUONG"), result.getLong("THANHTIEN"));
            listCT.add(ctPhieuBH);
        }
        return listCT;
    }
    
    public ArrayList<CTPhieuBanHangDTO> GetCTPhieuBanHangByMaSanPham(String MaSP) throws SQLException {
        ArrayList<CTPhieuBanHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaSP };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuBHByMaSPFromDatabase( ? )", objs);
        CTPhieuBanHangDTO ctPhieuBH;
        while (result.next()) {
            ctPhieuBH = new CTPhieuBanHangDTO(result.getString("MAPHIEUMH"), result.getString("MASP"), 
                    result.getInt("SOLUONG"), result.getLong("TONGTIEN"));
            listCT.add(ctPhieuBH);
        }
        return listCT;
    }
    
    public int UpdateCTPhieuBanHanh(CTPhieuBanHangDTO ctPhieuBH) {
        Object[] objs = { ctPhieuBH.getMaPhieuBH(), ctPhieuBH.getMaSP(), ctPhieuBH.getSoLuong(), 
                    ctPhieuBH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateCTPhieuBanHangToDatabase(?, ? ,?, ?)", objs);
    }
    
    public int DeleteCTPhieuBanHang(CTPhieuBanHangDTO ctPhieuBH) {
        Object[] objs = { ctPhieuBH.getMaPhieuBH(), ctPhieuBH.getMaSP() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteChiTietPMHToDatabase(?, ?)", objs);
    }
    
    public int InsertCTPhieuBanHang(CTPhieuBanHangDTO ctPhieuBH) {
        Object[] objs = { ctPhieuBH.getMaPhieuBH(), ctPhieuBH.getMaSP(), ctPhieuBH.getSoLuong(), 
                    ctPhieuBH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertChiTietPMHToDatabaseW(?, ? ,?, ?)", objs);
    }
}
